package com.replication.jdbc;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class ReplicationConnectionProxy implements Connection{
    private final Connection masterConn;
    private final List<Connection> slaverConns;
    private Iterator<Connection> slaverIt;
    private ConnectionSelector selector;

    public ReplicationConnectionProxy(Connection masterConn ,List<Connection> slaverConns,ConnectionSelector selector){
        this.masterConn = masterConn;
        this.slaverConns = slaverConns;
        this.selector=selector;
    }
    
    public ConnectionSelector getConnectionSelector(){
        return this.selector;
    }
    
    protected Connection getMasterConnection() {
        return masterConn;
    }
    
    protected synchronized Connection nextSlaverConnection(){
        if(this.slaverIt == null ||  (!slaverIt.hasNext())){
            this.slaverIt = this.slaverConns.iterator();
        }
        
        if(slaverIt.hasNext()){
            return slaverIt.next();
        }
        return masterConn;
    }
    
    @Override
    public void clearWarnings() throws SQLException {
        masterConn.clearWarnings();
    }

    @Override
    public void close() throws SQLException {
        closeConnection(masterConn);
        for(Connection slavConn : slaverConns){
            closeConnection(slavConn);
        }
    }
    
    private void closeConnection(Connection conn){
        try{
            conn.close();
        }catch(Exception e){
            
        }
    }

    @Override
    public void commit() throws SQLException {
        masterConn.commit();
    }

    @Override
    public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
        return masterConn.createArrayOf(typeName, elements);
    }

    @Override
    public Blob createBlob() throws SQLException {
        return masterConn.createBlob();
    }

    @Override
    public Clob createClob() throws SQLException {
        return masterConn.createClob();
    }

    @Override
    public NClob createNClob() throws SQLException {
        return masterConn.createNClob();
    }

    @Override
    public SQLXML createSQLXML() throws SQLException {
        return masterConn.createSQLXML();
    }

    @Override
    public Statement createStatement() throws SQLException {
        return new ReplicationStatementProxy(this);
    }

    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
        return new ReplicationStatementProxy(this,resultSetType,resultSetConcurrency);
    }

    @Override
    public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return new ReplicationStatementProxy(this,resultSetType,resultSetConcurrency,resultSetHoldability);
    }

    @Override
    public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
        return masterConn.createStruct(typeName, attributes);
    }

    @Override
    public boolean getAutoCommit() throws SQLException {
        return masterConn.getAutoCommit();
    }

    @Override
    public String getCatalog() throws SQLException {
        return masterConn.getCatalog();
    }

    @Override
    public Properties getClientInfo() throws SQLException {
        return masterConn.getClientInfo();
    }

    @Override
    public String getClientInfo(String name) throws SQLException {
        return masterConn.getClientInfo(name);
    }

    @Override
    public int getHoldability() throws SQLException {
        return masterConn.getHoldability();
    }

    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
        return masterConn.getMetaData();
    }

    @Override
    public int getTransactionIsolation() throws SQLException {
        return masterConn.getTransactionIsolation();
    }

    @Override
    public Map<String, Class<?>> getTypeMap() throws SQLException {
        return masterConn.getTypeMap();
    }

    @Override
    public SQLWarning getWarnings() throws SQLException {
        return masterConn.getWarnings();
    }

    @Override
    public boolean isClosed() throws SQLException {
        return masterConn.isClosed();
    }

    @Override
    public boolean isReadOnly() throws SQLException {
        return masterConn.isReadOnly();
    }

    @Override
    public boolean isValid(int timeout) throws SQLException {
        return masterConn.isValid(timeout);
    }

    @Override
    public String nativeSQL(String sql) throws SQLException {
        return masterConn.nativeSQL(sql);
    }

    @Override
    public CallableStatement prepareCall(String sql) throws SQLException {
        return masterConn.prepareCall(sql);
    }

    @Override
    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {

        return masterConn.prepareCall(sql, resultSetType, resultSetConcurrency);
    }

    @Override
    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return masterConn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
    }

    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        return Statements.prepareStatement(this, sql);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
        return Statements.prepareStatement(this, sql,autoGeneratedKeys);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
        return Statements.prepareStatement(this, sql,columnIndexes);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
        return Statements.prepareStatement(this, sql,columnNames);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        return Statements.prepareStatement(this, sql,resultSetType,resultSetConcurrency);
    }

    @Override
    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return Statements.prepareStatement(this, sql,resultSetType,resultSetConcurrency,resultSetHoldability);
    }

    @Override
    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
        masterConn.releaseSavepoint(savepoint);
    }

    @Override
    public void rollback() throws SQLException {
        masterConn.rollback();
    }

    @Override
    public void rollback(Savepoint savepoint) throws SQLException {
        masterConn.releaseSavepoint(savepoint);
    }

    @Override
    public void setAutoCommit(boolean autoCommit) throws SQLException {
        masterConn.setAutoCommit(autoCommit);
    }

    @Override
    public void setCatalog(String catalog) throws SQLException {
        masterConn.setCatalog(catalog);
    }

    @Override
    public void setClientInfo(Properties properties) throws SQLClientInfoException {
        masterConn.setClientInfo(properties);
    }

    @Override
    public void setClientInfo(String name, String value) throws SQLClientInfoException {
        masterConn.setClientInfo(name, value);
    }

    @Override
    public void setHoldability(int holdability) throws SQLException {
        masterConn.setHoldability(holdability);
    }

    @Override
    public void setReadOnly(boolean readOnly) throws SQLException {
        masterConn.setReadOnly(readOnly);
    }

    @Override
    public Savepoint setSavepoint() throws SQLException {
        return masterConn.setSavepoint();
    }

    @Override
    public Savepoint setSavepoint(String name) throws SQLException {
        return masterConn.setSavepoint(name);
    }

    @Override
    public void setTransactionIsolation(int level) throws SQLException {
        masterConn.setTransactionIsolation(level);
    }

    @Override
    public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
        masterConn.setTypeMap(map);
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return masterConn.isWrapperFor(iface);
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return masterConn.unwrap(iface);
    }

}
